﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PSI.Helper {
    public class ContainerHelper {
        /// <summary>
        /// 校验集装箱箱号是否正确
        /// </summary>
        /// <param name="containerNumber">集装箱箱号</param>
        /// <returns></returns>
        public bool ContainerNumberIsValide(string containerNumber) {
            if (string.IsNullOrEmpty(containerNumber)) {
                return false;
            }

            string comp, comp11;
            int totalvalue, totalvalue2, modvalue;
            int i;
            totalvalue = 0;
            totalvalue2 = 0;
            if (containerNumber.Length != 11) {
                return false;
            }
            for (i = 0; i < 10; i++) {
                comp = containerNumber.Substring(i, 1);
                switch (comp) {
                    case "A":
                        totalvalue = totalvalue + 10;
                        break;
                    case "B":
                        totalvalue = totalvalue + 12;
                        break;
                    case "C":
                        totalvalue = totalvalue + 13;
                        break;
                    case "D":
                        totalvalue = totalvalue + 14;
                        break;
                    case "E":
                        totalvalue = totalvalue + 15;
                        break;
                    case "F":
                        totalvalue = totalvalue + 16;
                        break;
                    case "G":
                        totalvalue = totalvalue + 17;
                        break;
                    case "H":
                        totalvalue = totalvalue + 18;
                        break;
                    case "I":
                        totalvalue = totalvalue + 19;
                        break;
                    case "J":
                        totalvalue = totalvalue + 20;
                        break;
                    case "K":
                        totalvalue = totalvalue + 21;
                        break;
                    case "L":
                        totalvalue = totalvalue + 23;
                        break;
                    case "M":
                        totalvalue = totalvalue + 24;
                        break;
                    case "N":
                        totalvalue = totalvalue + 25;
                        break;
                    case "O":
                        totalvalue = totalvalue + 26;
                        break;
                    case "P":
                        totalvalue = totalvalue + 27;
                        break;
                    case "Q":
                        totalvalue = totalvalue + 28;
                        break;
                    case "R":
                        totalvalue = totalvalue + 29;
                        break;
                    case "S":
                        totalvalue = totalvalue + 30;
                        break;
                    case "T":
                        totalvalue = totalvalue + 31;
                        break;
                    case "U":
                        totalvalue = totalvalue + 32;
                        break;
                    case "V":
                        totalvalue = totalvalue + 34;
                        break;
                    case "W":
                        totalvalue = totalvalue + 35;
                        break;
                    case "X":
                        totalvalue = totalvalue + 36;
                        break;
                    case "Y":
                        totalvalue = totalvalue + 37;
                        break;
                    case "Z":
                        totalvalue = totalvalue + 38;
                        break;
                    case "0":
                        totalvalue = totalvalue + 0;
                        break;
                    case "1":
                        totalvalue = totalvalue + 1;
                        break;
                    case "2":
                        totalvalue = totalvalue + 2;
                        break;
                    case "3":
                        totalvalue = totalvalue + 3;
                        break;
                    case "4":
                        totalvalue = totalvalue + 4;
                        break;
                    case "5":
                        totalvalue = totalvalue + 5;
                        break;
                    case "6":
                        totalvalue = totalvalue + 6;
                        break;
                    case "7":
                        totalvalue = totalvalue + 7;
                        break;
                    case "8":
                        totalvalue = totalvalue + 8;
                        break;
                    case "9":
                        totalvalue = totalvalue + 9;
                        break;
                }
                totalvalue2 = totalvalue2 + totalvalue * (int)Math.Pow(2, (double)i);
                totalvalue = 0;
            }
            modvalue = totalvalue2 % 11;
            if (modvalue == 10)
                modvalue = 0;
            comp11 = containerNumber.Substring(10, 1);
            if (modvalue != (int)(comp11[0]) - 48) {
                return false;
            }
            return true;
        }
    }
}